sd:一个直观,快速和用户友好的 sed 命令替代品。
安装:scoop install sd
# Find & replace in a file
$ sd 'window.fetch' 'fetch' http.js
# To preview changes:
$ sd -p 'window.fetch' 'fetch' http.js
# Indexed capture groups:
$ echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
cmd: cargo, channel: nightly, subcmd: watch
# Named capture groups:
$ echo "123.45" | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars dollars and $cents cents'
123 dollars and 45 cents
# In the unlikely case you stumble upon ambiguities, resolve them by using ${var} instead of $var.
$ echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars_dollars and $cents_cents'
and
$ echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '${dollars}_dollars and ${cents}_cents'
123_dollars and 45_cents
$ fd --type file --exec sd 'from "react"' 'from "preact"'
$ fd --type file --exec cp {} {}.bk \; --exec sd 'from "react"' 'from "preact"'
$ echo "./hello foo" | sd "foo" "-w"
error: Found argument '-w' which wasn't expected, or isn't valid in this context
USAGE:
sd [OPTIONS] <find> <replace-with> [files]...
For more information try --help
$ echo "./hello foo" | sd "foo" -- "-w"
./hello -w
$ echo "./hello --foo" | sd -- "--foo" "-w"
./hello -w